import React, { Component } from 'react';
import { Form, Row, Col, Icon } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sCustomerId: '', /* 客户名称(*) */
      sCustomerNo: '', /* 客户编号 */
      sParentId: '', /* 产品分类 */
      sProductName: '', /* 产品名称 */
      sProductStyle: '', /* 规格 */
      sProductUnit: '', /* 印件单位(*) */
      sCustomerProductNo: '', /* 客户料号 */
      sProductNo: '', /* 产品编号 */
      sMemo: '', /* 备注 */
      dMinStock: '', /* 最小库存 */
      dMaxStock: '', /* 最大库存 */
      reserveContent: '',
      expand: false,
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/eleproduct', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    } else if (sId === '' && this.props.newIdFlag) {
      const url = `${config.server_host}getUuid/${this.props.newIdlength}`;
      dispatch({
        type: 'content/getCopyDataId',
        payload: {
          sTabId, url, formRoute: '/eleproduct', componentName,
        },
      });
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sCustomerId: mainConfig.filter(item => (item.sName.toLowerCase() === 'scustomerid'))[0],
        sCustomerNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'scustomerno'))[0], /* 此为伪组件 */
        sParentId: mainConfig.filter(item => (item.sName.toLowerCase() === 'sparentid'))[0],
        sProductName: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductname'))[0],
        sProductStyle: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductstyle'))[0],
        sProductUnit: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductunit'))[0],
        sCustomerProductNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'scustomerproductno'))[0],
        sProductNo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sproductno'))[0],
        sMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'smemo'))[0],
        dMinStock: mainConfig.filter(item => (item.sName.toLowerCase() === 'dminstock'))[0],
        dMaxStock: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmaxstock'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
      this.setState({ sId });
    }
    const mainConfig = this.state.masterConfig.gdsconfigformslave;
    if (Object.keys(mainConfig).length > 0) {
      const { sActiveId, app, form } = nextProps;
      const childProps = {
        app, form, sActiveId, sId, dataList, disabled,
      };
      const reserveContent = commonFunc.Reserve(mainConfig, childProps);
      this.setState({ reserveContent });
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  handleToggle = () => {
    const { expand } = this.state;
    this.setState({ expand: !expand });
  };

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, handleSelectChange, onClose,
    } = this.props;
    const {
      dataList, sId, sActiveId, disabled, reserveContent, expand,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      handleSelectChange,
      dataName,
      dataType,
      onClose,
    };
    const {
      sCustomerId, sCustomerNo, sParentId, sProductName, sProductStyle, dMinStock, dMaxStock,
    } = this.state;
    const {
      sProductUnit, sCustomerProductNo, sProductNo, sMemo,
    } = this.state;
    const sCustomerIdProps = { showConfig: sCustomerId, ...childProps };
    const sCustomerNoProps = { showConfig: sCustomerNo, ...childProps };
    const sParentIdProps = { showConfig: sParentId, ...childProps };
    const sProductNameProps = { showConfig: sProductName, ...childProps };
    const sProductStyleProps = { showConfig: sProductStyle, ...childProps };
    const sProductUnitProps = { showConfig: sProductUnit, ...childProps };
    const sCustomerProductNoProps = { showConfig: sCustomerProductNo, ...childProps };
    const sProductNoProps = { showConfig: sProductNo, ...childProps };
    const sMemoProps = { showConfig: sMemo, ...childProps };
    const dMinStockProps = { showConfig: dMinStock, ...childProps };
    const dMaxStockProps = { showConfig: dMaxStock, ...childProps };
    return (
      <FormItem className={styles.searchMainForm}>
        <Row type="flex" style={{ height: expand ? 'auto' : '100px', overflow: 'hidden' }}>
          <Col
            span={6}
            order={sCustomerId.iOrder > 24 ? 24 : sCustomerId.iOrder}
            style={{ display: sCustomerId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sCustomerId" showDefaultName="客户名称" {...sCustomerIdProps} />
          </Col>
          <Col
            span={6}
            order={sCustomerNo.iOrder > 24 ? 24 : sCustomerNo.iOrder}
            style={{ display: sCustomerNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sCustomerNo" showDefaultName="客户编号" {...sCustomerNoProps} />
          </Col>
          <Col
            span={6}
            order={sParentId.iOrder > 24 ? 24 : sParentId.iOrder}
            style={{ display: sParentId.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sParentId" showDefaultName="产品分类" {...sParentIdProps} />
          </Col>
          <Col
            span={6}
            order={sProductName.iOrder > 24 ? 24 : sProductName.iOrder}
            style={{ display: sProductName.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductName" showDefaultName="产品名称" {...sProductNameProps} />
          </Col>
          <Col
            span={6}
            order={sProductStyle.iOrder > 24 ? 24 : sProductStyle.iOrder}
            style={{ display: sProductStyle.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductStyle" showDefaultName="规格" {...sProductStyleProps} />
          </Col>
          <Col
            span={6}
            order={dMinStock.iOrder > 24 ? 24 : dMinStock.iOrder}
            style={{ display: dMinStock.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMinStock" showDefaultName="最小库存" {...dMinStockProps} />
          </Col>
          <Col
            span={6}
            order={dMaxStock.iOrder > 24 ? 24 : dMaxStock.iOrder}
            style={{ display: dMaxStock.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="dMaxStock" showDefaultName="最大库存" {...dMaxStockProps} />
          </Col>
          <Col
            span={6}
            order={sProductUnit.iOrder > 24 ? 24 : sProductUnit.iOrder}
            style={{ display: sProductUnit.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductUnit" showDefaultName="印件单位" {...sProductUnitProps} />
          </Col>
          <Col
            span={6}
            order={sCustomerProductNo.iOrder > 24 ? 24 : sCustomerProductNo.iOrder}
            style={{ display: sCustomerProductNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sCustomerProductNo" showDefaultName="客户料号" {...sCustomerProductNoProps} />
          </Col>
          <Col
            span={6}
            order={sProductNo.iOrder > 24 ? 24 : sProductNo.iOrder}
            style={{ display: sProductNo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sProductNo" showDefaultName="产品编号" {...sProductNoProps} />
          </Col>
          <Col
            span={6}
            order={sMemo.iOrder > 24 ? 24 : sMemo.iOrder}
            style={{ display: sMemo.bVisible ? 'block' : 'none' }}
          >
            <ShowType sName="sMemo" showDefaultName="备注" textArea {...sMemoProps} />
          </Col>
          {reserveContent}
        </Row>
        <button className={styles.collapseA} onClick={this.handleToggle}>
          <span>{expand ? '缩略展示' : '显示全部'}</span><Icon type={expand ? 'up' : 'down'} />
        </button>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
